﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XcoAppSpaces.Contracts.Exceptions;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Serialization.Binary;
using XcoAppSpaces.Serialization.Json;

namespace XcoAppSpaces.Core.Configuration.Interpreters
{
	/// <summary>
	/// Config string interpreter for defining which serializer is used. Since
	/// the serializers themselves don't have any configurable values, this interpreter
	/// simply allows defining the used serializer by name.
	/// </summary>
	class SerializerConfigStringInterpreter : XcoConfigStringInterpreterBase
	{
		public const string ConfigSerializer = "serializer";

		private string _serializer;

		/// <summary>
		/// Action definition for interpretion
		/// </summary>
		public override Dictionary<string, Action<string>> InterpretDefinition
		{
			get
			{
				return new Dictionary<string, Action<string>> 
                    { 
                        {ConfigSerializer, str => _serializer = str}
                    };
			}
		}

		/// <summary>
		/// Returns the configured serializer
		/// </summary>
		public override IXcoService GetService
		{
			get
			{
				switch (_serializer)
				{
					case null:
						return null;
					case "binary":
						return new XcoBinarySerializer();
					case "json":
						return new XcoJsonSerializer();
					default:
						throw new XcoConfigurationException("Serializer name " + _serializer + " is unknown!");
				}
			}
		}
	}
}
